﻿using System.Threading;
using OpenWaves.ActivityFeed.RavenDB;
using OpenWaves.ActivityFeed.UnitTests;
using Raven.Client;

namespace OpenWaves.ActivityFeed.UnitTests_RavenDB
{
    class RavenDbActivityFeedServiceTestSession : IActivityFeedTestSession
    {
        private readonly IDocumentSession session;

        public RavenDbActivityFeedServiceTestSession(IDocumentSession session)
        {
            this.session = session;
        }

        public void Dispose()
        {
            this.session.Dispose();
        }

        public IActivityFeedService CreateActivityFeedService()
        {
            return new ActivityFeedService(this.session);
        }

        public void SaveChanges()
        {
            this.session.SaveChanges();
            // only for unit tests - wait for stale indexes to calculate
            while (this.session.Advanced.DocumentStore.DatabaseCommands.GetStatistics().StaleIndexes.Length != 0)
            {
                Thread.Sleep(10);
            }
        }
    }
}